package com.eii.common.web.page;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eii.common.web.domain.Result;
import com.eii.common.web.domain.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

import java.util.Collection;

/**
 * 分页实体类
 */
@ApiModel(description = "通用分页对象返回值包装")
public class PageResult<T> extends Result<Collection<T>> {

    /**
     * 总数
     */
    @Getter
    @ApiModelProperty(value = "总量", required = true)
    private Long total;

    private PageResult(Collection<T> data, Long total, String msg) {
        super(ResultCode.SUCCESS, data, msg);
        this.total = total;
    }

    /**
     * 返回分页查询类型结果(仅在成功时使用)<br>
     */
    public static <T> PageResult<T> page(Page<T> pageData) {
        return page(pageData.getRecords(), pageData.getTotal());
    }

    /**
     * 返回分页查询类型结果(仅在成功时使用)<br>
     */
    public static <T> PageResult<T> page(Collection<T> data, Long total) {
        return page(data, total, "查询成功");
    }

    /**
     * 返回分页查询类型结果(仅在成功时使用)<br>
     */
    public static <T> PageResult<T> page(Collection<T> data, Long total, String msg) {
        return new PageResult<>(data, total, msg);
    }

    public static <T> PageResult.PageResultBuilder<T> builder() {
        return new PageResult.PageResultBuilder<T>();
    }

    public static class PageResultBuilder<T> {
        private Collection<T> data;
        private String msg = "查询成功";
        private Long total;

        public PageResult.PageResultBuilder<T> data(Collection<T> data) {
            this.data = data;
            return this;
        }

        public PageResult.PageResultBuilder<T> total(Long total) {
            this.total = total;
            return this;
        }

        public PageResult.PageResultBuilder<T> msg(String msg) {
            this.msg = msg;
            return this;
        }

        public PageResult<T> build() {
            Assert.notNull(this.total);
            return PageResult.page(this.data, this.total, this.msg);
        }

    }
}
